package com.wangyadong.hobby.concurrent;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2018/10/2420:03
 **/
public class AtomicIntegerTest {

    /**
     * 1,可见性
     * 2.sequence
     * 3.no atmic
     */
//    private static volatile int value = 0;

    static Set<Integer> set = Collections.synchronizedSet(new HashSet<Integer>());

    public static void main(String[] args) throws InterruptedException {
//        Thread thread1 = new Thread() {
//            @Override
//            public void run() {
//                int x = 0;
//                while (x < 500) {
//                    int tmp = value;
//                    System.out.println(Thread.currentThread().getName() + "tmp:" + tmp);
//                    value += 1;
//                    set.add(value);
//                    x++;
//                }
//            }
//        };
//
//        Thread thread2 = new Thread() {
//            @Override
//            public void run() {
//                int x = 0;
//                while (x < 500) {
//                    int tmp = value;
//                    System.out.println(Thread.currentThread().getName() + "tmp:" + tmp);
//                    value += 1;
//                    set.add(value);
//                    x++;
//                }
//            }
//        };
//
//        Thread thread3 = new Thread() {
//            @Override
//            public void run() {
//                int x = 0;
//                while (x < 500) {
//                    int tmp = value;
//                    System.out.println(Thread.currentThread().getName() + "tmp:" + tmp);
//                    value += 1;
//                    set.add(value);
//                    x++;
//                }
//            }
//        };
//        thread1.start();
//        thread2.start();
//        thread3.start();
//        thread1.join();
//        thread2.join();
//        thread3.join();
//        System.out.println(set.size());

        AtomicInteger integer = new AtomicInteger();

        Thread thread3 = new Thread() {
            @Override
            public void run() {
                int x = 0;
                while (x < 500) {
                    int tmp = integer.getAndIncrement();
                    System.out.println(Thread.currentThread().getName() + "tmp:" + tmp);
                    set.add(tmp);
                    x++;
                }
            }
        };

        Thread thread2 = new Thread() {
            @Override
            public void run() {
                int x = 0;
                while (x < 500) {
                    int tmp = integer.getAndIncrement();
                    System.out.println(Thread.currentThread().getName() + "tmp:" + tmp);
                    set.add(tmp);
                    x++;
                }
            }
        };

        Thread thread1 = new Thread() {
            @Override
            public void run() {
                int x = 0;
                while (x < 500) {
                    int tmp = integer.getAndIncrement();
                    System.out.println(Thread.currentThread().getName() + "tmp:" + tmp);
                    set.add(tmp);
                    x++;
                }
            }
        };


        thread1.start();
        thread2.start();
        thread3.start();

        thread1.join();
        thread2.join();
        thread3.join();



        System.out.println("====" + set.size());
    }


}
